package com.ecoolex.discount.sales.core.service;

import com.ecoolex.discount.sales.core.external.v2.YWExternalService;
import com.ecoolex.discount.sales.core.external.v2.pojo.GetBrandsResponse;
import com.ecoolex.discount.sales.core.external.v2.pojo.GetGiftCardListResponse;
import com.ecoolex.discount.sales.core.external.v2.pojo.GetStoresResponse;
import com.ecoolex.discount.sales.core.manager.YWBrandManager;
import com.ecoolex.discount.sales.core.manager.YWGiftCardManager;
import com.ecoolex.discount.sales.core.manager.YWStoreManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.ecoolex.discount.sales.core.CoreConstant.YOU_WORLD_GIFTCARD_CHANNEL_FOREIGN_EXCHANGE;
import static com.ecoolex.discount.sales.core.CoreConstant.YOU_WORLD_GIFTCARD_CHANNEL_GLOBAL_BUY;

@Service
@Slf4j
public class YWService {

    @Autowired
    YWExternalService ywExternalService;

    @Autowired
    YWBrandManager ywBrandManager;

    @Autowired
    YWStoreManager ywStoreManager;

    @Autowired
    YWGiftCardManager ywGiftCardManager;


    private final List<String> CHANNELS = Arrays.asList(YOU_WORLD_GIFTCARD_CHANNEL_FOREIGN_EXCHANGE,
            YOU_WORLD_GIFTCARD_CHANNEL_GLOBAL_BUY);

    public void syncBrandData() {
        CHANNELS.forEach(channelId -> {
            String token = ywExternalService.getToken(channelId);
            GetBrandsResponse resp = ywExternalService.getBrands(token);
            if (Objects.nonNull(resp) && !CollectionUtils.isEmpty(resp.getData())) {
                resp.getData().forEach(ywBrandManager::save);
            }
        });

    }

    public void syncStoreData() {
        CHANNELS.forEach(channelId -> {
            String token = ywExternalService.getToken(channelId);
            GetStoresResponse resp = ywExternalService.getStores(token);
            if (Objects.nonNull(resp) && !CollectionUtils.isEmpty(resp.getData())) {
                resp.getData().forEach(ywStoreManager::save);
            }
        });
    }

    public void syncGiftCardListData() {
        CHANNELS.forEach(channelId -> {
            String token = ywExternalService.getToken(channelId);
            GetGiftCardListResponse resp = ywExternalService.getGiftCardList(token);
            if (Objects.nonNull(resp) && !CollectionUtils.isEmpty(resp.getData())) {
                List<GetGiftCardListResponse.GiftCard> data = resp.getData();
                data.forEach(ywGiftCardManager::save);
                List<Integer> cardIds = data.stream().map(GetGiftCardListResponse.GiftCard::getCardId).distinct().collect(Collectors.toList());
				/**
				 * 将下线的礼品卡设置成禁用
				 */
				ywGiftCardManager.disableOfflineCard(cardIds,"YouWorld",channelId);
            }
        });
    }

    public void processAsync() {
        ywExternalService.processAsync();
    }

}
